<template>
  <el-card style="width: 500px;padding: 20px;margin: 0 auto;">

    <el-form ref="form" :model="form" :rules="rules"  label-width="120px">
      <el-upload
          class="avatar-uploader"
          :action="'http://' + serverIp +':8081/file/upload'"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
      >
        <img class="header" v-if="form.avatarUrl" :src="form.avatarUrl" >
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      <el-form-item label="用户名">
        <el-input v-model="form.userName" disabled></el-input>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="form.nickName"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email"></el-input>
      </el-form-item>
      <el-form-item label="地址">
        <el-input type="textarea" v-model="form.address"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="save">确 定</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import {serverIp} from "../../../public/config";

export default {
  name: "Person",
  data(){
    return{
      serverIp: serverIp,
      form:{
        phone: '',
        email: '',
      },
      rules: {
        email: [
          { type: 'email', message: '请输入正确的邮箱地址'}
        ],
        phone: [
          { type : "string",pattern : /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
            ,message: '请输入正确手机号' },
        ]
      },
      user:localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
    }
  },
  created() {
    this.getUser().then(res => {
      this.form = res
    })
  },
  methods:{
    async getUser(){
      return (await this.request.get("user/username/"+this.user.userName)).data
    },
    save(){
      this.request.post("/user",this.form).then(res => {
        if (res.code === 200 ){
          this.$message.success("保存成功")
          // 触发父级更新User的方法
          this.$emit("refreshUser")
          // 更新浏览器存储的用户信息
          this.getUser().then(res => {
            res.token = JSON.parse(localStorage.getItem("user")).token
            localStorage.setItem("user", JSON.stringify(res))
          })
        }else {
          this.$message.error("保存失败")
        }
      })
    },
    handleAvatarSuccess(res) {
      this.form.avatarUrl = res
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 5;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  }
}
</script>

<style scoped>

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.header{
  width: 100px;
  height: 100px;
  position: relative;
  margin-left: 220px;
  border-radius: 50%;
}
</style>